import { fetchList, refreshTable } from ".";
import { meassageMap } from "@userList/utils";
import { drawerLoading, drawerVisible } from "./drawer";
import {
  drawerFormState,
  operateType,
  popularBackgroundColorOptions,
  tableActiveKey,
  vipLevelColorOptions,
  vipValidMap,
} from "./store";
import { tableLoading } from "./table";
export const fetchScoreDataById = async (record: any = {}) => {
  try {
    tableLoading.value = true;
    const res: any = await gradeApi.queryScoreById({
      infoId: record?.id,
    });
    if (res.code != 200) return message.error(res.message);
    const { info, access } = res?.data ?? {};
    drawerFormState.id = info?.id;
    drawerFormState.level = info?.level;
    drawerFormState.icon = info?.icon;
    drawerFormState.name = info?.name;
    drawerFormState.title = info?.title;
    drawerFormState.status = info?.status == 0 ? 0 : 1;
    drawerFormState.rule = info?.rule;
    drawerFormState.award = info?.award;
    drawerFormState.privateClub = access?.privateClub?.value ?? 0;
    drawerFormState.privateService = access?.privateService?.value ?? 0;
    drawerFormState.onlineHideFlag = access?.onlineHideFlag?.value ?? 0;
    drawerFormState.clubHideMemberFlag = access?.clubHideMemberFlag?.value ?? 0;
    drawerFormState.hideLocationFlag = access?.hideLocationFlag?.value ?? 0;
    drawerFormState.bubbleChatFlag = access?.bubbleChatFlag?.value ?? 0;
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};

export const fetchPopularDataById = async (record: any = {}) => {
  try {
    tableLoading.value = true;
    const res: any = await gradeApi.queryPopularById({
      infoId: record?.id,
    });
    if (res.code != 200) return message.error(res.message);
    const { info, colorType = {} } = res?.data ?? {};
    popularBackgroundColorOptions.value = objectToOptions(colorType);
    if (operateType.value === "update") {
      drawerFormState.id = record.id;
      drawerFormState.level = info?.level;
      drawerFormState.icon = info?.icon;
      drawerFormState.url = info?.url;
      drawerFormState.name = info?.name;
      drawerFormState.title = info?.title;
      drawerFormState.status = info?.status;
      drawerFormState.rule = info?.rule;
      drawerFormState.color = info?.color ? info?.color : undefined;
    }
    if (operateType.value === "add") {
      drawerFormState.color = popularBackgroundColorOptions.value?.[0]?.value;
    }
  } catch (error: any) {
    message.error(error.message);
  } finally {
    tableLoading.value = false;
  }
};
export const fetchAddFriendIsMember = async () => {
  try {
    const res = await gradeApi.queryAddFriendIsMember();
    drawerFormState.addFriendIsMember =
      +res.data.addFriendIsMember === 0 ? true : false;
  } catch (error: any) {
    message.error(error.message);
  }
};
export const fetchVipDataById = async (record: any = {}) => {
  try {
    const res: any = await gradeApi.queryVipById({
      typeId: record?.id,
    });
    if (res.code != 200) return message.error(res.message);
    const { memberType, access, colorType = {}, indate = {} } = res?.data ?? {};
    vipLevelColorOptions.value = objectToOptions(colorType);
    vipValidMap.value = objectToOptions(indate, "number");
    if (operateType.value === "update") {
      drawerFormState.level = memberType?.level;
      drawerFormState.id = memberType?.id;
      drawerFormState.url = memberType?.url;
      drawerFormState.src = memberType?.src;
      drawerFormState.icon = memberType?.icon;
      drawerFormState.name = memberType?.name;
      drawerFormState.indate = memberType?.indate;
      drawerFormState.price = memberType?.price;
      drawerFormState.status = memberType?.status;
      drawerFormState.color = memberType?.color ? memberType?.color : undefined;
      drawerFormState.addFriendNum = access?.addFriendNum?.value ?? 0;
      drawerFormState.addContactNum = access?.addContactNum?.value ?? 0;
      drawerFormState.redPacketNum = access?.redPacketNum?.value ?? 0;
      drawerFormState.giftNum = access?.giftNum?.value ?? 0;
      drawerFormState.userFilterFlag = access?.userFilterFlag?.value ?? 0;
      drawerFormState.privateService = access?.privateService?.value ?? 0;
      drawerFormState.whoSeenMeFlag = access?.whoSeenMeFlag?.value ?? 0;
      drawerFormState.offlinePartyFlag = access?.offlinePartyFlag?.value ?? 0;
      drawerFormState.allPhotoFlag = access?.allPhotoFlag?.value ?? 0;
      drawerFormState.privateMedal = access?.privateMedal?.value ?? 0;
      drawerFormState.worldChannelVoice = access?.worldChannelVoice?.value ?? 0;
      drawerFormState.chatBubble = access?.chatBubble?.value ?? 0;
    }

    if (operateType.value === "add") {
      drawerFormState.color = vipLevelColorOptions.value?.[0]?.value;
      drawerFormState.indate = +vipValidMap.value[0].value;
    }
  } catch (error: any) {
    message.error(error.message);
  }
};

const defineSubmitApiMap = (string, number, formState) => {
  const basicForm = {
    name: formState.name,
    icon: formState.icon,
    status: formState.status,
  };
  const basicMemberForm = {
    src: formState.src,
    price: formState.price,
    indate: formState.indate,
    accessData: {
      addFriendNum: formState.addFriendNum,
      addContactNum: formState.addContactNum,
      // userFilterFlag: +formState.userFilterFlag,
      privateService: +formState.privateService,
      redPacketNum: formState.redPacketNum,
      giftNum: formState.giftNum,
      whoSeenMeFlag: +formState.whoSeenMeFlag,
      offlinePartyFlag: +formState.offlinePartyFlag,
      allPhotoFlag: +formState.allPhotoFlag,
      privateMedal: +formState.privateMedal,
      worldChannelVoice: +formState.worldChannelVoice,
      chatBubble: +formState.chatBubble,
    },
  };
  const basicMemberAndPopularForm = {
    url: formState.url,
    color: formState.color,
  };
  const basicPopularAndScoreForm = {
    rule: formState.rule,
  };
  const basicPopularForm = {
    title: formState.title,
  };
  const basicScoreForm = {
    award: formState.award,
    accessData: {
      privateClub: +formState.privateClub,
      privateService: +formState.privateService,
      onlineHideFlag: +formState.onlineHideFlag,
      clubHideMemberFlag: +formState.clubHideMemberFlag,
      hideLocationFlag: +formState.hideLocationFlag,
      bubbleChatFlag: +formState.bubbleChatFlag,
    },
  };
  const createMember = () =>
    gradeApi.createVip({
      ...basicForm,
      ...basicMemberForm,
      ...basicMemberAndPopularForm,
    });
  const updateMember = () =>
    gradeApi.updateVip({
      editId: formState.id,
      ...basicForm,
      ...basicMemberForm,
      ...basicMemberAndPopularForm,
    });
  const createPopular = () =>
    gradeApi.createPopular({
      ...basicForm,
      ...basicMemberAndPopularForm,
      ...basicPopularAndScoreForm,
      ...basicPopularForm,
    });
  const updatePopular = () =>
    gradeApi.updatePopular({
      editId: formState.id,
      ...basicForm,
      ...basicMemberAndPopularForm,
      ...basicPopularAndScoreForm,
      ...basicPopularForm,
    });
  const createScore = () =>
    gradeApi.createScore({
      ...basicForm,
      ...basicPopularAndScoreForm,
      ...basicScoreForm,
    });
  const updateScore = () =>
    gradeApi.updateScore({
      editId: formState.id,
      ...basicForm,
      ...basicPopularAndScoreForm,
      ...basicScoreForm,
    });
  const apiMap = new Map([
    [{ key: "add", state: 0 }, createMember],
    [{ key: "update", state: 0 }, updateMember],
    [{ key: "add", state: 1 }, createPopular],
    [{ key: "update", state: 1 }, updatePopular],
    [{ key: "add", state: 2 }, createScore],
    [{ key: "update", state: 2 }, updateScore],
  ]);
  return apiMap;
};

const handleSubmitApiTask = async (string, number = 0, formState) => {
  try {
    let [, api]: any = [...defineSubmitApiMap(string, number, formState)].find(
      ([object]) => object.key == string && object.state == number
    );
    drawerLoading.value = true;
    const result: any = await api();
    if (result.code != 200) return message.error(result.message);
    drawerVisible.value = false;
    const [, showMessage] = [...meassageMap(refreshTable, fetchList)].find(
      ([key]) => key.test(string)
    );
    showMessage();
  } catch (e: any) {
    message.error(e.message);
  } finally {
    drawerLoading.value = false;
  }
};
export const handleSubmit = () =>
  handleSubmitApiTask(operateType.value, tableActiveKey.value, drawerFormState);
